﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net


Partial Class mtFundCenter
    Inherits System.Web.UI.Page

    Private Shared logger As ILog = LogManager.GetLogger("mtFundCenter")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >>  FundCenter"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session.Remove("SearchTxt")
            Session("SearchTxt") = ""
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        Else
            ' PlanYearsTxt.Text = Session("PlanYearTxt")
        End If
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        If Session("SearchTxt") = "" Then
            Exit Sub
        End If
        Dim adapter As New MasterDataSetTableAdapters.FundCentersTableAdapter
        Dim dtTable As New MasterDataSet.FundCentersDataTable

        Dim sql As String = "select * from FundCenters "
        sql += " WHERE 1 = 1 "

        sql += Session("SearchTxt")

        sql += " ORDER BY FundCenterCode,PlanYear "

        adapter.SearchFundCenter(dtTable, sql)

        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("FundCenterCode").AllowDBNull = True
        dtTable.Columns("Description").AllowDBNull = True

        dtTable.Columns("EvmCode").AllowDBNull = True
        dtTable.Columns("StrategyCode").AllowDBNull = True
        dtTable.Columns("ResponsibleAnalystCode").AllowDBNull = True
        dtTable.Columns("FundCenterTypeCode").AllowDBNull = True
        dtTable.Columns("ProfitCenterCode").AllowDBNull = True
        dtTable.Columns("BusinessAreaCode").AllowDBNull = True

        e.DataSource = dtTable
        'Response.Write(dtTable.Count)
        'WebGrid1.ClientAction.Alert(sql)
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(2).ForceNoEdit = True
        e.Row.Cells(10).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.FundCentersTableAdapter
        Dim check = adapter.CheckExistFundCenters(e.Row.Cells(1).Value, e.Row.Cells(2).Value)

        If Val(check) <> 0 Then
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ข้อมูล Fund Center ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim id As Decimal
            id = adapter.InsertFCenter(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, Date.Now, Session("users"), _
                                        If(e.Row.Cells(4).Value = "", " ", e.Row.Cells(4).Value), If(e.Row.Cells(5).Value = "", " ", e.Row.Cells(5).Value), _
                                        If(e.Row.Cells(6).Value = "", " ", e.Row.Cells(6).Value), If(e.Row.Cells(7).Value = "", " ", e.Row.Cells(7).Value), _
                                        If(e.Row.Cells(8).Value = "", " ", e.Row.Cells(8).Value), If(e.Row.Cells(9).Value = "", " ", e.Row.Cells(9).Value))

            'id = adapter.InsertQueryxx(e.Row.Cells(1).Value, Val(e.Row.Cells(2).Value), e.Row.Cells(3).Value, Date.Now, Session("users"))
            Dim dr As DataRow = e.Row.DataRow
            dr("id") = id
            '' WebGrid1.ClientAction.Refresh()

            logger.Info("User " & Session("users") & " insert fund center " & e.Row.Cells(1).Value)
        End If

        'Dim dr As DataRow = e.Row.DataRow
        'dr("FundCenterCode") = id

    End Sub
    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)

        Dim id As String
        id = e.Row.KeyValue

        Dim adapter As New MasterDataSetTableAdapters.FundCentersTableAdapter
        Dim check = adapter.CheckExistUpdate(e.Row.Cells(1).Value, e.Row.Cells(2).Value, id)

        If Val(check) = 1 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูล Fund Center ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim adap2 As New MasterDataSet2TableAdapters.checkuseTableAdapter
            Dim dt As MasterDataSet.FundCentersDataTable
            dt = adapter.GetDataById(Val(id))
            Dim oldcode As String = dt.Item(0).FundCenterCode
            Dim oldyear As Integer = dt.Item(0).PlanYear
            Dim used As Integer = adap2.CheckFCTR(oldcode, oldyear)
            If oldcode.Trim = e.Row.Cells(1).Value.ToString.Trim And oldyear = Val(e.Row.Cells(2).Value) Then
                used = 0
            End If
            If used = 0 Then
                adapter.UpdateFCenter(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, Date.Now, Session("users"), _
                                        If(e.Row.Cells(4).Value = "", " ", e.Row.Cells(4).Value), If(e.Row.Cells(5).Value = "", " ", e.Row.Cells(5).Value), _
                                        If(e.Row.Cells(6).Value = "", " ", e.Row.Cells(6).Value), If(e.Row.Cells(7).Value = "", " ", e.Row.Cells(7).Value), _
                                        If(e.Row.Cells(8).Value = "", " ", e.Row.Cells(8).Value), If(e.Row.Cells(9).Value = "", " ", e.Row.Cells(9).Value), id)
                'adapter.UpdateQueryxx(e.Row.Cells(1).Value, Val(e.Row.Cells(2).Value), e.Row.Cells(3).Value, Date.Now, Session("users"), id)
                logger.Info("User " & Session("users") & " update fund center " & e.Row.Cells(1).Value)
            Else
                WebGrid1.ClientAction.Refresh()
                WebGrid1.ClientAction.Alert("ไม่สามารแกไขข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
            End If
        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then

            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim code As String = TryCast(Request("code"), String)
            Dim yr As String = TryCast(Request("year"), String)

            Dim adapter As New MasterDataSetTableAdapters.FundCentersTableAdapter
            Dim adap2 As New MasterDataSet2TableAdapters.checkuseTableAdapter

            Dim used As Integer = adap2.CheckFCTR(code, Val(yr))
            'MsgBox(used)
            If type = "1" Then

                Try
                    If used = 0 Then
                        adapter.DeleteFCenter(Deletet)
                        logger.Info("User " & Session("users") & " delete fund center " & Deletet)
                    Else
                        WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                    End If

                Catch ex As Exception
                    WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
                End Try

            ElseIf type = "2" Then
                Try
                    If used = 0 Then
                        For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                            adapter.DeleteFCenter(keyValue)
                            logger.Info("User " & Session("users") & " delete fund center " & keyValue)
                        Next keyValue
                    Else
                        WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลบางรายการได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                    End If
                Catch ex As Exception
                    WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
                End Try
            End If
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("SearchTxt") = ""
        If PlanYearsTxt.Text <> "" Then
            Session("SearchTxt") += " and PlanYear =  " & Val(PlanYearsTxt.Text)
        End If
        If CodeTxt.Text <> "" Then
            Session("SearchTxt") += " and FundCenterCode = '" & CodeTxt.Text & "'"
        End If
        If SearchTxt.Text <> "" Then
            Session("SearchTxt") += " and Description like '%" & SearchTxt.Text & "%'"
        End If
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Protected Sub EvmCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles EvmCombo.InitializeDataSource
        Dim adap As New MasterDataSetTableAdapters.EvmTableAdapter
        Dim dt As New MasterDataSet.EvmDataTable
        dt = adap.GetDataByPlanYear(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub
    Protected Sub StrategyCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles StrategyCombo.InitializeDataSource
        Dim adap As New MasterDataSetTableAdapters.StrategyTableAdapter
        Dim dt As New MasterDataSet.StrategyDataTable
        dt = adap.GetDataByPlanYear(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub
    Protected Sub ResponsibleAnalystCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ResponsibleAnalystCombo.InitializeDataSource
        Dim adap As New MasterDataSetTableAdapters.ResponsibleAnalyst1TableAdapter
        Dim dt As New MasterDataSet.ResponsibleAnalyst1DataTable
        dt = adap.GetData(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub
    Protected Sub FundCenterTypeCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterTypeCombo.InitializeDataSource
        Dim adap As New MasterDataSetTableAdapters.FundCenterTypeTableAdapter
        Dim dt As New MasterDataSet.FundCenterTypeDataTable
        dt = adap.GetDataByPlanYear(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub
    Protected Sub ProfitCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ProfitCenterCombo.InitializeDataSource
        Dim adap As New MasterDataSetTableAdapters.ProfitCenterTableAdapter
        Dim dt As New MasterDataSet.ProfitCenterDataTable
        dt = adap.GetDataByPlanYear(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub
    Protected Sub BusinessAreaCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles BusinessAreaCombo.InitializeDataSource
        Dim adap As New MasterDataSetTableAdapters.BusinessAreaTableAdapter
        Dim dt As New MasterDataSet.BusinessAreaDataTable
        dt = adap.GetDataByPlanYear(Val(PlanYearsTxt.Text))
        e.DataSource = dt
    End Sub
End Class
